﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TelephoneYiShou
{
    public partial class Jhdd : Skin_Mac
    {
        public DataTable dt;
        int a, start;//a表示总行数，start表示当前位置
        int size = 6;//size表示一页显示的数量
        string str1 = "select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID";
        string str = "server=.;database = TelephoneYiShou;uid = sa;pwd = 123456";
        public Jhdd()
        {
            InitializeComponent();
        }

        //显示数据
        private void Jhdd_Load(object sender, EventArgs e)
        {
            //显示数据
            DGV_Rk.DataSource = DBHelper.GetDataTable("select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID");
            dt = DBHelper.GetDataTable(str1);
            a = dt.Rows.Count;//获取数据的条数
            show(0, size);
            label1.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, a / size + 1, a);//显示数据

            //商品id下拉框绑定     
            string sqlname = "select distinct commodityname from commodity";
            DataTable dte = DBHelper.GetDataTable(sqlname);
            for (int i = 0; i < dte.Rows.Count; i++)
            {
                comid.Items.Add(dte.Rows[i][0]);
            }

            //供应商id下拉框绑定
            string sqlclname = "select distinct Suppliername from Supplier";
            DataTable cldt = DBHelper.GetDataTable(sqlclname);
            for (int i = 0; i < cldt.Rows.Count; i++)
            {
                comboBox2.Items.Add(cldt.Rows[i][0]);
            }
        }
        //下一页
        private void button1_Click(object sender, EventArgs e)
        {

            if (start + 1 > a / size)
            {
                MessageBoxEx.Show("已是尾页");
                show(start * size, size);
            }
            else
            {
                start = start + 1;
                show(start * size, size);
            }
            label1.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, a / size + 1, a);//显示数据
        }

        //上一页
        private void button2_Click(object sender, EventArgs e)
        {
            if (start < 1)
            {
                MessageBoxEx.Show("已是第一页");
                show(0, size);
            }
            else
            {
                start = start - 1;
                show(start * size, size);
            }
            label1.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, a / size + 1, a);//显示数据

        }

        private void color_MouseEnter(object sender, EventArgs e)
        {
            //颜色数据绑定
            color.Items.Clear();
            string sqlcolor = string.Format("select colour from commodity where commodityname = '{0}'", comid.Text);
            DataTable dtes = DBHelper.GetDataTable(sqlcolor);
            for (int i = 0; i < dtes.Rows.Count; i++)
            {
                color.Items.Add(dtes.Rows[i][0]);
            }
        }
        //修改
        private void btn_update_Click(object sender, EventArgs e)
        {
            if (la.Text == "请选择")
            {
                MessageBoxEx.Show("请先选择修改的订单");
                return;
            }
            string orderid = null;          //货物编号
            string supplierID = null;  //供应商编号
            //查询商品编号
            if (!string.IsNullOrEmpty(comid.Text))
            {
                string sqls = string.Format("select commodityid from commodity where commodityname='{0}'", comid.Text);
                orderid = DBHelper.GetDataTable(sqls).Rows[0][0].ToString();
            }
            //查询供应商编号
            if (!string.IsNullOrEmpty(comboBox2.Text))
            {
                string sqls = string.Format("select supplierid from supplier where suppliername='{0}'", comboBox2.Text);
                supplierID = DBHelper.GetDataTable(sqls).Rows[0][0].ToString();
            }
            string num = txt_num.Text;           //订货数量
            string date = txt_date.Text;        //订货日期
            string remark = txt_remark.Text;   //备注
            string money = txt_money.Text;    //订单总金额
            string pay = txt_paystyle.Text;  //支付方式
            if (string.IsNullOrEmpty(orderid) || string.IsNullOrEmpty(supplierID) || string.IsNullOrEmpty(num) || string.IsNullOrEmpty(date) || string.IsNullOrEmpty(remark) || string.IsNullOrEmpty(money) || string.IsNullOrEmpty(pay))
            {
                MessageBoxEx.Show("请填写完整的数据");
                return;
            }
            string cid = la.Text;
            string usql =string.Format("update OrderGoods set CommodityID = '{0}',SupplierID = '{1}',Getnumber = '{2}',OrderTime= '{3}',Paytype = '{4}',Remark= '{5}',Moneysum = '{6}' where orderid = '{7}'", orderid, supplierID, num, Convert.ToDateTime(date), pay, remark, money, cid);
            if (DBHelper.ExecuteNonQuery(usql))
            {
                MessageBoxEx.Show("修改成功");
                //刷新DGV
                str1 = "select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID";
                dt = DBHelper.GetDataTable(str1);
                a = dt.Rows.Count;//获取数据的条数
                show(0, size);
                label10.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, a / size + 1, a);//显示数据
            }
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (la.Text == "请选择")
            {
                MessageBoxEx.Show("请先选择删除的订单");
                return;
            }
            string cid = la.Text;
            string dsql = string.Format("delete ordergoods where orderid = '{0}'",cid);
            if (DBHelper.ExecuteNonQuery(dsql))
            {
                MessageBoxEx.Show("删除成功");
                //刷新DGV
                str1 = "select OrderID ,c.CommodityName cn,c.colour '颜色',s.SupplierName sn,Getnumber,OrderTime,Paytype,Remark,Moneysum from OrderGoods o,Commodity c,Supplier s where o.CommodityID = c.CommodityID and o.SupplierID = s.SupplierID";
                dt = DBHelper.GetDataTable(str1);
                a = dt.Rows.Count;//获取数据的条数
                show(0, size);
                label10.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, a / size + 1, a);//显示数据
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DGV_Rk_Click(object sender, EventArgs e)
        {
            la.Text = DGV_Rk.SelectedRows[0].Cells[0].Value.ToString();
            comid.Text = DGV_Rk.SelectedRows[0].Cells[1].Value.ToString();
            color.Text = DGV_Rk.SelectedRows[0].Cells[2].Value.ToString();
            comboBox2.Text = DGV_Rk.SelectedRows[0].Cells[3].Value.ToString();
            txt_num.Text = DGV_Rk.SelectedRows[0].Cells[4].Value.ToString();
            txt_paystyle.Text = DGV_Rk.SelectedRows[0].Cells[6].Value.ToString();
            txt_remark.Text = DGV_Rk.SelectedRows[0].Cells[7].Value.ToString();
            txt_money.Text = DGV_Rk.SelectedRows[0].Cells[8].Value.ToString();
        }

        private void txt_money_MouseEnter(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(comid.Text) && !string.IsNullOrEmpty(color.Text) && !string.IsNullOrEmpty(txt_num.Text))
                {
                    string idsql = string.Format("select commodityid from commodity where commodityname = '{0}' and colour = '{1}'", comid.Text, color.Text);
                    string txtgoodsid = DBHelper.GetDataTable(idsql).Rows[0][0].ToString();
                    string ssmsql = string.Format("select ordergoodsprice from commodity where commodityid = '{0}'", txtgoodsid);
                    DataTable sumdt = DBHelper.GetDataTable(ssmsql);
                    double sums = double.Parse(sumdt.Rows[0][0].ToString()) * double.Parse(txt_num.Text);
                    txt_money.Text = sums.ToString();
                }
                else
                {
                    txt_money.Text = null;
                }
            }
            catch (Exception)
            {

                MessageBoxEx.Show("请查看你的输入是否错误");
            }
        }

        //show方法
        public void show(int i, int j)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
            SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqlada.Fill(ds, i, j, "[stock]");//把数据库表中的i-j的记录数用来填充数据集
            DGV_Rk.DataSource = ds.Tables["[stock]"];
            ds = null;//清空数据集
        }
    }
    }


